﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.Timers" %>
<%@ Import Namespace="System.Threading" %>   
<%@ Import Namespace="System.Workflow.Activities"%>
<%@ Import Namespace="System.Workflow.Runtime"%>
<%@ Import Namespace="System.Workflow.Runtime.Hosting"%>
<%@ Import Namespace="System.Diagnostics"%>
<%@ Import Namespace="WebKpiPage.BLL.GongdanMgr"%>
<script runat="server">
    private static System.Threading.Timer timer = null;   
    void Application_Start(object sender, EventArgs e) 
    {
        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = new System.Workflow.Runtime.WorkflowRuntime(WFWorkHelper.WorkflowRuntimeName);
        // Add system SQL state service
        string ConnectionStr = WFWorkHelper.ConnectionString;
        SqlWorkflowPersistenceService stateService = new SqlWorkflowPersistenceService(ConnectionStr, true, new TimeSpan(0, 2, 0), new TimeSpan(0, 0, 5));

        //RetrieveNonblockingInstanceStateIds
        workflowRuntime.AddService(stateService);

        Application[WFWorkHelper.WorkflowRuntimeName] = workflowRuntime;
        //映射事件没用上
        workflowRuntime.WorkflowCompleted += new EventHandler<System.Workflow.Runtime.WorkflowCompletedEventArgs>(workflowRuntime_WorkflowCompleted);
        workflowRuntime.WorkflowTerminated += new EventHandler<System.Workflow.Runtime.WorkflowTerminatedEventArgs>(workflowRuntime_WorkflowTerminated);
        workflowRuntime.WorkflowSuspended += new EventHandler<System.Workflow.Runtime.WorkflowSuspendedEventArgs>(workflowRuntime_WorkflowSuspended);
        workflowRuntime.WorkflowPersisted += new EventHandler<System.Workflow.Runtime.WorkflowEventArgs>(workflowRuntime_WorkflowPersisted);
        workflowRuntime.WorkflowIdled += new EventHandler<System.Workflow.Runtime.WorkflowEventArgs>(workflowRuntime_WorkflowIdled);

        // 绑定服务
        workflowRuntime.StartRuntime();

        foreach (SqlPersistenceWorkflowInstanceDescription workflowDesc
            in ((SqlWorkflowPersistenceService)stateService).GetAllWorkflows())
        {
           WorkflowInstance instance= workflowRuntime.GetWorkflow(workflowDesc.WorkflowInstanceId);
       
           string stateStr= (new StateMachineWorkflowInstance(workflowRuntime, instance.InstanceId)).CurrentStateName;
           System.Diagnostics.Trace.WriteLine(stateStr);
        }


        //计算现在到目标时间要过的时间段。   
        NameValueCollection config = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("customGroup/custom");
        string[] timeStr = config["alarmTime"].Split(':');
        int hour = 0;
        int minute = 0;
        int second = 0;
        int.TryParse(timeStr[0], out hour);
        int.TryParse(timeStr[0], out hour);
        int.TryParse(timeStr[0], out hour);
        DateTime LuckTime = DateTime.Now.Date.Add(new TimeSpan(hour, minute, second));
        TimeSpan span = LuckTime - DateTime.Now;
        if (span < TimeSpan.Zero)
        {
            span = LuckTime.AddDays(1d) - DateTime.Now;
        }
        //按需传递的状态或者对象。   
        object state = new object();
        //定义计时器   
        timer = new System.Threading.Timer(
        new TimerCallback(CertainTask), state,
        span, TimeSpan.FromTicks(TimeSpan.TicksPerDay));
        //////////   测试一下   ----------------

        CertainTask("");
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        if (timer != null) { timer.Dispose(); }
        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application[WFWorkHelper.WorkflowRuntimeName] as System.Workflow.Runtime.WorkflowRuntime;
        workflowRuntime.StopRuntime();
    }  
    private static void CertainTask(object state)
    {
         GongdanLogic.AutoMakeGongdan();
        // GongdanLogic.GetPingGuStatsGongdan(7,6);
        // GongdanLogic.GetPingGuStatsGongdan(20,8);
       // GongdanMgr.GetPingGuStatsGongdan(1,1);
       //  Thread thread = new Thread(new ParameterizedThreadStart( GongdanLogic.GetPingGuStatsGongdan));
     //    thread.Start(5);
      //   Thread thread2 = new Thread(new ParameterizedThreadStart(GongdanLogic.GetPingGuStatsGongdan));
        // thread2.Start(6);
    }  
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application[WFWorkHelper.WorkflowRuntimeName] as System.Workflow.Runtime.WorkflowRuntime;
        if (!workflowRuntime.IsStarted)
        {
            workflowRuntime.StartRuntime();
        }
        //

    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        System.Workflow.Runtime.WorkflowRuntime workflowRuntime = Application[WFWorkHelper.WorkflowRuntimeName] as System.Workflow.Runtime.WorkflowRuntime;
        if (workflowRuntime.IsStarted)
        {
            workflowRuntime.StopRuntime();
        }


    }


    protected void workflowRuntime_WorkflowPersisted(object sender, System.Workflow.Runtime.WorkflowEventArgs e)
    {
        System.Console.WriteLine(string.Concat("WorkflowPersisted:", e.WorkflowInstance.InstanceId));
        Trace.WriteLine(string.Concat("WorkflowPersisted:", e.WorkflowInstance.InstanceId));
    }

    protected void workflowRuntime_WorkflowSuspended(object sender, System.Workflow.Runtime.WorkflowSuspendedEventArgs e)
    {
        Trace.WriteLine(string.Concat("WorkflowSuspended:", e.Error, ",", e.WorkflowInstance.InstanceId));
    }

    protected void workflowRuntime_WorkflowTerminated(object sender, System.Workflow.Runtime.WorkflowTerminatedEventArgs e)
    {
        Trace.WriteLine(string.Concat("WorkflowTerminated:", e.Exception.ToString()));
        Trace.WriteLine("WorkflowTerminated:Owner" + DateTime.Now);
    }

    protected void workflowRuntime_WorkflowCompleted(object sender, System.Workflow.Runtime.WorkflowCompletedEventArgs e)
    {
        System.Diagnostics.Trace.WriteLine(string.Concat("WorkflowCompleted:", e.WorkflowInstance.InstanceId));
    }

    protected void workflowRuntime_WorkflowIdled(object sender, System.Workflow.Runtime.WorkflowEventArgs e)
    {
        
        ((System.Workflow.Runtime.WorkflowInstance)e.WorkflowInstance).TryUnload();
    }

       
</script>
